#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
//我的头文件群
#include "./res/m.h"

#define DISP_BUF_SIZE (480 * 1024)

//---------------------------


//----------------------------

int main(void)
{
    /*lvgl初始化*/
    lv_init();

    /*输出设备初始化及注册*/
    fbdev_init();
    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];
    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);
    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb   = fbdev_flush;
    disp_drv.hor_res    = 800;
    disp_drv.ver_res    = 480;
    lv_disp_drv_register(&disp_drv);

    //输入设备初始化及注册
    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;
    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb = evdev_read;
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);

    //官方demo---可以换为自己的demo
    //lv_demo_widgets();

    //施工重地
//=============================================================================
    
    // 背景显示
    // 图片路径的方法，路径为图片在开发板上的位置
    lv_obj_t *win = lv_obj_create(lv_scr_act());
    lv_obj_set_size(win, 800, 480); 
    // 禁用滚动 
    lv_obj_clear_flag(win, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_t *img = lv_img_create(win);   
    lv_img_set_src(img , "S:/root/al1.bmp");
    // 确保图片尺寸与屏幕匹配
    lv_obj_set_size(img, 800, 480); 
    // 图片居中对齐
    lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
    
    
    int led_ctrl_flag = -1;
    
    
    // 时间显示模块 
    time_display(win);
    // 天气显示模块
    char *rcv_buf1 = weather_display(win);
    // 空气检测显示模块
    char *rcv_buf2 = air_testing(win);

    // 调试所使用的伪造数据
    //char *rcv_buf1 = "humidity:40\ntemperature:25\n";
    //char *rcv_buf2 = "CityJC:GZS\nPM2.5:50\nQuality:good\n";
    
    printf("天气显示:%s\n",rcv_buf1);
    printf("空气检测:%s\n",rcv_buf2);
    
    if(strcmp(rcv_buf1,"humidity:5")>0)
    {
        printf("湿度已超百分之50,开灯\n");
        led_ctrl_flag = 1;
    }
    
    else if(strcmp(rcv_buf1,"humidity:5")<=0)
    {
        printf("湿度低于百分之50,关灯\n");
        led_ctrl_flag = 0;
    }
    
    // LED控制模块
    LED_ctrl(win, led_ctrl_flag, rcv_buf1); 
    // 数据上报模块
    data_reporting(win, rcv_buf1, rcv_buf2);
    
    

 //=============================================================================
 
    /*事物处理及告知lvgl节拍数*/
    while(1) 
    {
        lv_timer_handler();//事务处理
        lv_tick_inc(5);//节拍累计
        usleep(5000);
    }

    return 0;
}


/*用户节拍获取*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}
